jQuery(function () {
    
    ////获得客户信息列表
    var serverData = {};
    serverData.key='';
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#customerTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/customer/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                	data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
    	            	var id=data.id
    	            	var operate ='<input type="checkbox" class="checkchild">';
    	            	return operate;
    	            }   
                },
                {
                    data: 'customerCode'
                },
                {
                    data: 'customerName',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'saleArea.superiorArea'
                },
                {
                	data: 'business'
                },
                {
                	data: 'superior',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
						}
					}
                },
                {
                	data: 'buyer'
                },
                {
                	data: 'creditLevel'
                },
				{
					data : 'auditState',
					render : function(data,
							type, full,
							meta) {
						var dOut;
						if (data == '已审核') {
							dOut = '<td class="center" ><span class="badge label-pill label-primary">已审核</span></td>';
						} else{
							dOut = '<td class="center" ><span class="badge label-pill label-danger">未审核</span></td>';
						}
						return dOut;
					}
				} 
                
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        var deleteCustomer = function (data) {
            if (!data) return;
            $.springLemonDialog({
                content: '确认客户：' + data.customerName + '？',//确认文本
                clickButton: function (sender, modal, index) {//按钮事件处理
                    //index:0 确认    1：取消
                    //sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
                    if (index == 0) {
                        var url =window.rootPath+ "/api/customers/" + data.id;
                        jQuery.ajax({
                            url: encodeURI(url),
                            cache: false,
                            type: "delete",
                            dataType: "json",
                            success: function (data) {
                                serverTable.draw(false);
                            }
                        });
                    }
                    $(this).closeDialog(modal);
                }
            });
            
        };
        
    });
    ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
    /////新增客户
    jQuery(".addBtn").on("click",function(){
    	window.location = window.rootPath+"/ui/customer/add";
    });
    
    
    ////编辑客户
    jQuery('.editBtn').on('click', function () {
    	var selects=getSelect("#customerTable",1);
    	var url2 = window.rootPath + "/ui/customer/shCustomer?id="+selects[0].id;
    	 if(selects[0].auditState == "已审核"){
	   	 var d=
	   	 {
	   		"a": selects[0].id
	   	 }
	   	 jQuery.ajax({
	   			url : encodeURI(url2),
	   			type : 'post',
	   			data : JSON.stringify(d),
	   			dataType : 'json',
	   			contentType : 'application/json;charset=utf-8',
	   			success : function(data) {
	   				if(data.sata == 2){
	   					$.springLemonDialog({
	       	 		         content: '对不起 ,当前账户不具备编辑客户功能！',// 确认文本
	       	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
	       	 		             $(this).closeDialog(modal);
	       	 		         }
	       	 		   }); 
	   				}else{
	   					//var url=window.rootPath+"/ui/customer/delcustomer";
	   					var url = window.rootPath+ '/ui/customerAllocationIndex/Allgetpage';
	   					editSelect(selects,url,serverTable) 
	   				}
	   				serverTable.draw();
	   			}
	   		}); 
    	 }else{
    		 var url = window.rootPath+ '/ui/customerAllocationIndex/Allgetpage';
    		 editSelect(selects,url,serverTable) 
    	 }
    	
 	   /*var selects=getSelect("#customerTable",1);
 	   if(selects.length>1){
 		   $.springLemonDialog({
 		         content: '只能编辑一条客户',// 确认文本
 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 		             $(this).closeDialog(modal);
 		         }
 		   }); 
 	   }
 	   else if(selects.length==0){
 		   $.springLemonDialog({
 		         content: '请选择要编辑的记录',// 确认文本
 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 		             $(this).closeDialog(modal);
 		         }
 		   }); 
 	   }else if(selects.length==1){
 		 if(selects[0].auditState == "已审核"){
 			 var url = window.rootPath+ '/ui/customerAllocationIndex/Allgetpage';
 			 var d=
			 {
				"key": ''
			 }
			 jQuery.ajax({
					url : encodeURI(url),
					type : 'get',
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						if(data.recordsTotal<=0){
							$.springLemonDialog({
				 		         content: '你不具有编辑该数据的权限！',// 确认文本
				 		         clickButton: function (sender, modal, index) {// 按钮事件处理
				 		             $(this).closeDialog(modal);
				 		         }
				 		   });
							window.location = window.rootPath+"/ui/customer";
							return;
						}
					}
				}); 
 		 }
 		 var customerId=selects[0].id;
 		 window.location = window.rootPath+"/ui/customer/edit"+"?customerId="+customerId;
 	   }*/	    
    });
    function editSelect(selects,url,tableName){
    	var selects=getSelect("#customerTable",1);
  	   if(selects.length>1){
  		   $.springLemonDialog({
  		         content: '只能编辑一条客户',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }
  	   else if(selects.length==0){
  		   $.springLemonDialog({
  		         content: '请选择要编辑的记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }else if(selects.length==1){
  		 if(selects[0].auditState == "已审核"){
  			 //var url = window.rootPath+ '/ui/customerAllocationIndex/Allgetpage';
  			 var d=
 			 {
 				"key": ''
 			 }
 			 jQuery.ajax({
 					url : encodeURI(url),
 					type : 'get',
 					data : JSON.stringify(d),
 					dataType : 'json',
 					contentType : 'application/json;charset=utf-8',
 					success : function(data) {
 						if(data.recordsTotal<=0){
 							$.springLemonDialog({
 				 		         content: '你不具有编辑该数据的权限！',// 确认文本
 				 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 				 		             $(this).closeDialog(modal);
 				 		         }
 				 		   });
 							window.location = window.rootPath+"/ui/customer";
 							return;
 						}
 					}
 				}); 
  		 }
  	   }
  		 var customerId=selects[0].id;
  		 window.location = window.rootPath+"/ui/customer/edit"+"?customerId="+customerId;
    };
    ///查看客户
    jQuery('.seeBtn').on('click', function () {
 	   var selects=getSelect("#customerTable",1);
 	   if(selects.length>1){
 		   $.springLemonDialog({
 		         content: '只能查看一条记录',// 确认文本
 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 		             $(this).closeDialog(modal);
 		         }
 		   }); 
 	   }
 	   else if(selects.length==0){
 		   $.springLemonDialog({
 		         content: '请选择要查看的记录',// 确认文本
 		         clickButton: function (sender, modal, index) {// 按钮事件处理
 		             $(this).closeDialog(modal);
 		         }
 		   }); 
 	   }else if(selects.length==1){
 		 var customerId=selects[0].id;
 		 window.location = window.rootPath+"/ui/customer/see"+"?customerId="+customerId;
 	   }	    
    });
    ///查看客户
    jQuery('.updateBtn').on('click', function () {
    	var url2 = window.rootPath + "/ui/customer/updataSareaName";
    	jQuery.ajax({
			url : encodeURI(url2),
			type : 'get',
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				serverTable.draw();
			}
		}); 
    });
    /////删除客户/////
    jQuery('.delBtn').on('click', function () {
 	   var selects=getSelect("#customerTable",1);
 	  var url2 = window.rootPath + "/ui/customer/shCustomer?id="+selects[0].id;
 	  if(selects[0].auditState == "已审核"){
	 var d=
	 {
		"a": selects[0].id
	 }
	 jQuery.ajax({
			url : encodeURI(url2),
			type : 'post',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				if(data.sata == 2){
					$.springLemonDialog({
    	 		         content: '对不起 ，当前账户 不具备删除客户功能！',// 确认文本
    	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
    	 		             $(this).closeDialog(modal);
    	 		         }
    	 		   }); 
				}else{
					var url=window.rootPath+"/ui/customer/delcustomer";
				 	   delSelect(selects,url,serverTable) 
				}
				serverTable.draw();
			}
		}); 
 	  }else{
 		 var url=window.rootPath+"/ui/customer/delcustomer";
	 	   delSelect(selects,url,serverTable) 
 	  }	        	 
 	  
 	  
 	  
 	  
 	  
 	  
 	  
 	  
 	  
 	 // submitSelect(selects,url2,serverTable)
 	   
 	});
    //客户查询
	jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	})
	// 撤销客户查询
	jQuery('.cancelBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
	//审核客户
	jQuery('.shBth').on('click', function() {
    	var selects=getSelect("#customerTable",1);
    	var url = window.rootPath + "/ui/customer/shCustomer?id="+selects[0].id;
 	   submitSelect(selects,url,serverTable) 
    });
	function submitSelect(selects,url,tableName){
		if(selects.length >1){
			$.springLemonDialog({
   	         content: '每次只能审核一条信息！',// 确认文本
   	         clickButton: function (sender, modal, index) {// 按钮事件处理
   	             $(this).closeDialog(modal);
   	         }
   	       }); 
		return;
		}
    	if(selects.length==0){
    	       $.springLemonDialog({
    	         content: '请选择要审核的信息',// 确认文本
    	         clickButton: function (sender, modal, index) {// 按钮事件处理
    	             $(this).closeDialog(modal);
    	         }
    	       }); 
    	   }else{
    		   var auditStates=selects[0].auditState;
    		   var operate ="0";
			   if(selects[0].auditState == "已审核"){
				   operate ="1";
			   }
    		   if(operate == 1){
    	  			$.springLemonDialog({
    	 		         content: '已经审核过的信息不可重复操作！！',// 确认文本
    	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
    	 		             $(this).closeDialog(modal);
    	 		         }
    	 		   });  
    	  		 }
    		   else{
        		   $.springLemonDialog({
        		         content: '确认提交审核'+selects.length+'条信息？',// 确认文本
        		         //content: '确认提交该信息？',// 确认文本
        		         clickButton: function (sender, modal, index) {// 按钮事件处理
        		        	 if (index == 0) {
        		        			 var d=
        		        			 {
        		        				"a": selects[0].id
        		        			 }
        		        			 jQuery.ajax({
        									url : encodeURI(url),
        									type : 'post',
        									data : JSON.stringify(d),
        									dataType : 'json',
        									contentType : 'application/json;charset=utf-8',
        									success : function(data) {
        										if(data.sata == 2){
        											$.springLemonDialog({
        						    	 		         content: '对不起 ，当前账户不具备审核客户功能！',// 确认文本
        						    	 		         clickButton: function (sender, modal, index) {// 按钮事件处理
        						    	 		             $(this).closeDialog(modal);
        						    	 		         }
        						    	 		   }); 
        										}
        										serverTable.draw();
        									}
        								}); 
        		        	 }
        		             $(this).closeDialog(modal);
        		         }
        		       });  
    		   }
    		     
    	   }
    	
    }
    
});